//
//  TRFindViewController.m
//  WeiMiLan
//
//  Created by anan on 14/11/11.
//  Copyright (c) 2014年 Mac. All rights reserved.
//

#import "TRFindViewController.h"
#import "TRAdsWebController.h"

@interface TRFindViewController ()

{
    CGRect _rectViewContent;
    CGRect _rectViewPager;
    CGRect _rectViewPagerSC;
    CGRect _rectFragmentView;
}



@end

@implementation TRFindViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loadAdsWeb:) name:@"kLoadAds" object:nil];
    
    NSDictionary *userInfo = [[QLUserManager shareInstance] getLastUserInfoFromNative];
    NSString *userId=[userInfo objectForKey:@"ID"];
    [[NSUserDefaults standardUserDefaults] setObject:userInfo forKey:@"userId"];
   
    if (userInfo) {
        NSString *headerUrl = [userInfo ql_stringForKey:@"HEAD_PIC_URL"];
        if (headerUrl==nil||[headerUrl rangeOfString:@"/null"].location!=NSNotFound) {
            UIAlertView *alert = [[UIAlertView alloc] init];
            [alert setTitle:@"提示"];
            [alert setMessage:@"请更新昵称和头像"];
            [alert addButtonWithTitle:@"确定"];
            [alert addButtonWithTitle:@"忽略"];
            alert.tag=1;
            alert.delegate = self;
            [alert show];
        }
        
        NSString *phoneNum = [userInfo ql_stringForKey:@"PHONE"];
        BOOL b = [QLPropertyUtils getBoolByKey:@"firstInstall" defaultValue:YES];
        BOOL alertPhone = [QLPropertyUtils getBoolByKey:@"alertPhone" defaultValue:NO];
        if ((phoneNum==nil||phoneNum.length<11)&&b==NO&&alertPhone==NO) {
            [QLPropertyUtils putBoolByKey:@"alertPhone" value:YES];
            UIAlertView *alert = [[UIAlertView alloc] init];
            [alert setTitle:@"提示"];
            [alert setMessage:@"请绑定手机，以免丢失账号"];
            [alert addButtonWithTitle:@"确定"];
            [alert addButtonWithTitle:@"忽略"];
            alert.tag=2;
            alert.delegate = self;
            [alert show];
        }else{
            [self showGuide];
        }
        [QLPropertyUtils putBoolByKey:@"firstInstall" value:NO];
    }
    [self initNavigation];
}

- (void)loadAdsWeb:(NSNotification *)noti {
    NSString *url = noti.userInfo[@"web"];
    TRAdsWebController *adsVC = [[TRAdsWebController alloc]init];
    if ([url containsString:@"http"]) {
       adsVC.adsUrl = url;
    }else {
        url = [NSString stringWithFormat:@"http://%@",url];
        adsVC.adsUrl = url;
    }
  //  self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:adsVC animated:YES];
}

- (void)showGuide
{
    if ([QLPropertyUtils getBoolByKey:@"guide_find" defaultValue:NO]==NO) {
        self.viewGuide.hidden = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(guideTap:)];
        [self.viewGuide addGestureRecognizer:tap];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    
    NSLog(@"TRFindViewController");
    
    CGRect r = CGRectMake(9, 24, 36, 36);
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:r];
    NSDictionary *ui = [[QLUserManager shareInstance] getLastUserInfoFromNative];
    if ([[ui ql_stringForKey:@"HEAD_PIC_URL"] containsString:@"upload/users"]) {
        //   NSString *str = ApplicationDelegate.urlHearder;
        NSString *str = [[NSUserDefaults standardUserDefaults]objectForKey:@"urlHeader"];
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"nodata_avator"]];
    } else {
        [imageView sd_setImageWithURL:[NSURL URLWithString:[ui ql_stringForKey:@"HEAD_PIC_URL"]] placeholderImage:[UIImage imageNamed:@"nodata_avator"]];
    }
    imageView.clipsToBounds = YES;
    imageView.layer.cornerRadius = 18;
    [self.weChatNavigationBar addSubview:imageView];
}

- (void)fullScreen
{
    if (CGRectIsEmpty(_rectViewContent)) {
        _rectViewContent = self.viewContent.frame;
        _rectViewPager = self.viewPager.frame;
        _rectViewPagerSC = self.viewPager.scrollView.frame;
    }
    
    CGRect r = CGRectMake(0, 0, _rectViewContent.size.width, _rectViewContent.size.height+59);// bei 1125
    self.viewContent.frame = r;
    self.weChatNavigationBar.hidden = YES;
  //  self.weChatNavigationBar.opaque = NO;
  //  [self.weChatNavigationBar removeFromSuperview];
    
  //  NSLog(@"%d-------",self.weChatNavigationBar.isHidden);
    
    self.viewPager.frame = CGRectMake(0, 0, r.size.width,r.size.height);
  //  NSLog(@"%@----------------",NSStringFromCGRect(self.viewPager.frame));
    self.viewPager.scrollView.frame = self.viewPager.frame;
    self.viewPager.tabPageIndicator.hidden = YES;
    self.viewPager.scrollView.scrollEnabled = NO;
}

- (void)fullScreenExit
{
    if (CGRectIsEmpty(_rectViewContent)) {
        _rectViewContent = self.viewContent.frame;
        _rectViewPager = self.viewPager.frame;
        _rectViewPagerSC = self.viewPager.scrollView.frame;
    }
    
    CGRect r = _rectViewContent;
    self.viewContent.frame = r;
    self.weChatNavigationBar.hidden = NO;
 //   [self.view addSubview:self.weChatNavigationBar];
   // self.weChatNavigationBar.opaque = NO;
   // self.weChatNavigationBar.backgroundColor = [UIColor greenColor];
   // [self.view insertSubview:self.weChatNavigationBar belowSubview:self.viewGuide];
    self.viewPager.frame = _rectViewPager;
    self.viewPager.scrollView.frame = _rectViewPagerSC;
    self.viewPager.tabPageIndicator.hidden = NO;
    self.viewPager.scrollView.scrollEnabled = YES;
    TRFindNewsListView *view = (TRFindNewsListView*)self.viewPager.currentFragment.contentView;
    if (CGRectIsEmpty(_rectFragmentView)) {
        _rectFragmentView = view.frame;
    }
    r = view.frame;
    r.size.height = _rectFragmentView.size.height;
    view.frame = r;
}

#pragma mark --UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==1) {
        if (buttonIndex == 0) {
            TRUserDetailViewController *vc = [[TRUserDetailViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    if (alertView.tag==2) {
        if (buttonIndex == 0) {
            TRUserDetailViewController *vc = [[TRUserDetailViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    [self showGuide];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (self.viewPager==nil) {
        [self initViewPager];
    }else{
        NSDictionary *ui = [[QLUserManager shareInstance] getLastUserInfoFromNative];
        NSString *account = [ui ql_stringForKey:@"LOGIN_NAME"];
        
        if (self.currentAccount) {
            if ([account isEqualToString:self.currentAccount]==NO) {
                [self refreshNews];
            }
        }
        self.currentAccount = account;
    }
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    //[[QLRecordPlayer shareInstance] stop];
}

- (void)refreshNews
{
    TRFindNewsListView *view = (TRFindNewsListView*)([self.adapter getFragment:self.viewPager position:0].contentView);
    [view refresh];
}

- (void)initViewPager
{
    CGRect rect = CGRectMake(0, 0, self.viewContent.frame.size.width, self.viewContent.frame.size.height);
    self.viewPager = [[QLViewPager alloc]initWithFrame:rect];
    self.viewPager.pageIndicatorHeight = 30;
    self.viewPager.backgroundColor = [UIColor whiteColor];//[UIColor colorWithWhite:0.092 alpha:1.000];
    self.viewPager.scrollView.backgroundColor = [UIColor clearColor];
    self.viewPager.showPageIndicator = YES;
    [self.viewPager setup];
    [self.viewContent addSubview:self.viewPager];
    self.adapter = [[TRFindPagerFragmentAdapter alloc] initWithViewController:self];
    [self.viewPager setAdapter:self.adapter];
    
    self.viewPager.delegate = self;
    [self onPageChange:self.viewPager select:self.viewPager.currentIndex];
}

- (void)guideTap:(id)sender
{
    self.viewGuide.hidden = YES;
    [QLPropertyUtils putBoolByKey:@"guide_find" value:YES];
}

- (void)initNavigation
{
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.weChatNavigationBar=[[WeChatNavigationBar alloc] init];
    [self.view insertSubview:self.weChatNavigationBar belowSubview:self.viewGuide];
    self.weChatNavigationBar.titleLabel.text=@"";
    [self.weChatNavigationBar.leftButton addTarget:self action:@selector(shouLeftVC) forControlEvents:UIControlEventTouchUpInside];
    [self.weChatNavigationBar.leftButton setImage:nil forState:UIControlStateNormal];
    
  
    
    [self.weChatNavigationBar.rightButton setImage:[UIImage imageNamed:@"seachBT"] forState:0];
    [self.weChatNavigationBar.rightButton addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
    self.weChatNavigationBar.rightButton.frame=CGRectMake(kUI_SCREEN_WIDTH-46, 30, 23, 23);

}

-(void)shouLeftVC
{
  //  [self addChildViewController:ApplicationDelegate.ddVC];
    [ApplicationDelegate.ddVC showLeftController:YES];
}

-(void)search:(id)btn{
    UIViewController * vc = [[TRSearchProductViewController alloc] init];
  //  self.hidesBottomBarWhenPushed = YES;
 //   self.tabBarController.tabBar.hidden =  YES;
    [self.navigationController pushViewController:vc animated:YES];
  //  [self presentViewController:vc animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)removeTopButton{
    
}

-(void)visibleTop{
    
}

#pragma mark --QLViewPagerDelegate
- (void)onPageChange:(QLViewPager *)viewPager select:(int)select
{
   
       
}

@end
